/*
 * Copyright (c) 2014 laidian. All Rights Reserved.
 * @author yaoyuming
 * @date  2020-01-08 14:20
 */
package com.yao.activiti.service.impl;

import cn.hutool.core.lang.Console;
import com.yao.activiti.entity.vo.ReProdefVo;
import com.yao.activiti.mapper.ReProcdefMapper;
import com.yao.activiti.service.DeployService;
import com.yao.activiti.service.IReProcdefService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 资源服务类
 *
 * @author yaoyuming
 * @date 2020/01/08 14:20
 */
@Service
public class DeployServiceImpl implements DeployService {

    @Autowired
    private ProcessEngine processEngine;

    @Autowired
    private IReProcdefService reProcdefService;

    @Autowired
    private ReProcdefMapper mapper;


    @Override
    public ProcessDefinition deploy(String fileName) {

        return this.deploy(fileName,null);
    }

    @Override
    public ProcessDefinition deploy(String fileName, String name) {
        return this.deploy(fileName, name, null);
    }

    @Override
    public ProcessDefinition deploy(String fileName, String name, String key) {
        return this.deploy(fileName, name, key, null);
    }

    @Override
    public ProcessDefinition deploy(String fileName, String name, String key, String category){
        DeploymentBuilder deploymentBuilder = ProcessEngines.getDefaultProcessEngine().getRepositoryService().createDeployment();

        deploymentBuilder.name(name).key(key).category(category);

        deploymentBuilder.addClasspathResource("processes" +  File.separator + fileName + ".bpmn")
                .addClasspathResource("processes" + File.separator +  fileName + ".png");

        Deployment deploy = deploymentBuilder.deploy();

        Console.log("部署ID [{}], 部署名称 [{}]:", deploy.getId(), deploy.getName());

        ProcessDefinition processDefinition = ProcessEngines.getDefaultProcessEngine().getRepositoryService().createProcessDefinitionQuery().deploymentId(deploy.getId()).singleResult();

        Console.log("流程定义详情：{}",processDefinition);
        return processDefinition;
    }


    @Override
    public void list() {
        List<ReProdefVo> list = mapper.list();
        Map<String, List<ReProdefVo>> collect  = list.stream()
                .sorted(Comparator.comparing(ReProdefVo::getProdefVersion).reversed())
                .collect(Collectors.groupingBy(ReProdefVo::getCategory, Collectors.toList()));
        System.out.println("collect = " + collect);
        list.stream().forEach(System.out::println);
//        List<Deployment> deployments = ProcessEngines.getDefaultProcessEngine().getRepositoryService()
//                .createDeploymentQuery()
//                .orderByDeploymenTime()
//                .desc()
//                .listPage(0, 10);
//
//        list.stream().forEach(e -> {
//            Console.log("id = {}, name = {}, deployTime = {}, category = {}, key = {}, tenantId = {}",e.getId(), e.getName(), e.getDeploymentTime(), e.getCategory(), e.getKey(), e.getTenantId());
//        });
    }
}